var contextPath = $("#contextPath").val();
var selectIndex=0;//选中的金额位置，默认为0
var zhifuMethod="zhifubao";//支付方式,默认为支付宝
$(function(){
	new Tab('.vip_select',{
	    itemClass:'.vip_type_item',
	    active:'vip_select_active',
	    onClick: function (tab) {
	      var index = tab.index;
	      var elem = tab.id.querySelector('.enterTips')
	      var eleinput = tab.id.querySelector('.enterInput')
	      if(index === 5){
	         elem.classList.add('dn')
	         eleinput.classList.remove('dn')
	         eleinput.focus()
	         eleinput.addEventListener('input',function(){
	           this.value = this.value.trim().replace(/\D/,'');
	         },false);
	      }else{
	        elem.classList.remove('dn')
	        eleinput.classList.add('dn')
	      }
	      selectIndex=index; 
	    }
	  });
	form.Radio('.pay_mode_item');
	
	
	//如果选择支付宝支付则支付方式为zhifubao
	$("#zhifubao").click(function(){
		zhifuMethod="zhifubao";
	}
	);
	//如果选择微信支付则支付方式为weixin
	$("#weixin").click(function(){
	zhifuMethod="weixin";
	});
  //提交
  $("#recharge").click(function(){
		var otherPrice=$("#otherPrice").val();//其他金额
		var chargePrice=(selectIndex==0?500:selectIndex==1?1000:selectIndex==2?3000:selectIndex==3?10000:selectIndex==4?30000:otherPrice);//充值金额
		//如果支付方式为支付宝，则跳转到支付宝支付页面，否则跳转到微信支付页面
		if(zhifuMethod=="zhifubao"){
		window.location.href=contextPath+"/recharge/confirm_alipay_recharge?price="+chargePrice;
		}else{
		$.ajax({
			url:contextPath+"/recharge/confirm_wxjs_recharge",
			type:"post",
			async:true,
			data:{"price":chargePrice},
			dataType:"json",
			success : function(data, textStatus) {
				var state = data.result.state;
				console.log(data);
				if(state == 1){
					doWxJsPay(data.prePayInfo);
				}else{
					layer.open({
					    title: '提示',
					    content: data.result.message,
					    btn: ['我知道了']
					});
				}
				layer.close(load);
			},
			error : function(XMLHttpRequest, textStatus, errorThrown){
			}
		});	
		}
	});

  function doWxJsPay(prePayInfo){
  	if (typeof WeixinJSBridge == "undefined"){
  		   if( document.addEventListener ){
  		       document.addEventListener('WeixinJSBridgeReady', function(){
  		    	   onBridgeReady(prePayInfo);
  		       }, false);
  		   }else if (document.attachEvent){
  		       document.attachEvent('WeixinJSBridgeReady', function(){
  		    	   onBridgeReady(prePayInfo);
  		       }); 
  		       document.attachEvent('onWeixinJSBridgeReady', function(){
  		    	   onBridgeReady(prePayInfo);
  		       });
  		   }
  		}else{
  		   onBridgeReady(prePayInfo);
  		}
  }
  function onBridgeReady(prePayInfo){
  	WeixinJSBridge.invoke('getBrandWCPayRequest',prePayInfo,
  		    function(res){
  		       //支付成功或失败前台判断
      	       if(res.err_msg=='get_brand_wcpay_request:ok'){
      	    	   layer.open({
  					    title: '提示',
  					    content: '恭喜你，充值成功！',
  					    btn: ['我知道了'],
  					    yes: function(index){
  					        layer.close(index);
  					        location.href=contextPath+"/user";
  					    }
  					});	
      	       }else if(res.err_msg=='get_brand_wcpay_request:cancel'){	   
      	    	   layer.open({
  					    title: '提示',
  					    content: '支付已取消！',
  					    btn: ['我知道了']
  					});	
      	       }else{
      	    	   layer.open({
  					    title: '提示',
  					    content: '支付失败，请重试！',
  					    btn: ['我知道了']
  					});	
      	       }
  		     });
  	
  }
});
